package org.pp.controller;


import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.pp.core.*;
import org.pp.dto.PageDto;
import org.pp.entity.PpUserAddress;
import org.pp.miniapp.dto.UserAddressAdd;
import org.pp.miniapp.dto.UserAddressEdite;
import org.pp.service.IPpUserAddressService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/address")
@Slf4j
public class PpUserAddressController extends BaseCrudController<PpUserAddress,
        UserAddressAdd, UserAddressEdite,PpUserAddress
        >
    implements
        BaseDetail<PpUserAddress>,
        BaseDelete,
        BasePage<PpUserAddress>
{

    @Autowired
    IPpUserAddressService addressService;


    @Override
    @PostMapping("/add")
    @Operation(summary = "添加")
    public Result<Object> add(@RequestBody  UserAddressAdd dto) {

       Long addressId= addressService.add(dto);
       return Result.ok(addressId);
    }

    @Override
    public Result<PpUserAddress> detail(Long id) {
        return super.detail(id);
    }

    @Override
    public Result<Void> delete(String ids) {
        //if(!Objects.equals(MiniappSessionUtil.getUserId(),))
        addressService.delete(ids);
        return Result.ok();
    }

    @Override
    @PostMapping("/edite")
    @Operation(summary = "编辑")
    public Result<Void> edite(@RequestBody  UserAddressEdite dto) {
       addressService.edite(dto);
       return Result.ok();
    }

    @Override
    @GetMapping("/list")
    public Result<PageBean<PpUserAddress>> page(@ParameterObject PageDto page) {
        return super.page(page);
    }


    @GetMapping("/default")
    @Operation(summary = "获取默认地址")
    public Result<PpUserAddress> defaultAddress() {
        return Result.ok(addressService.defaultAddress());
    }

}
